package com.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.biz.NewsBiz;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;
import com.util.GuGuaUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jc on 2019/11/16.
 */
@RestController("apiNewsController")
@RequestMapping("/api/news")
public class NewsController extends BaseController<News> {
    @Autowired
    private NewsBiz newsBiz;


    //首页广告列表
    @RequestMapping(value = "/bannerList")
    public Result bannerList(@RequestParam Integer sortId) {
        if (sortId == null) {
            return ResultGenerator.genFailResult("广告类型不能为空");
        }
        Condition condition = new Condition(News.class);
        condition.createCriteria().andEqualTo("sortId", sortId);
        List<News> news = newsBiz.findByCondition(condition);
        return ResultGenerator.genSuccessResult(news);
    }


    @RequestMapping(value = "/fpHash")
    public Result fpHash(@RequestParam String getStr) {
        if (getStr == null) {
            return ResultGenerator.genFailResult("内容不能为空");
        }

        String mid = "44840993862";
        String key = "C218565A9B05693C6B93E2BE0DCAD2CCFA933149";

        Date date = new Date();
        JSONObject jsonObject = JSON.parseObject(getStr);
        ArrayList s = new ArrayList();
        s.add(jsonObject.getString("amount"));
        s.add(jsonObject.getString("curr"));
        s.add(Long.toString(date.getTime()));
        s.add("Order " + Long.toString(date.getTime()));
        s.add(mid);
        s.add(jsonObject.getString("timestamp"));
        s.add(jsonObject.getString("nonce"));
        s.add(GuGuaUtil.fp_hash(s, key));

        return ResultGenerator.genSuccessResult(s);
    }
}
